//
//  SortSegmentView.m
//  YouHui
//
//  Created by  on 11-12-1.
//  Copyright (c) 2011年 netease. All rights reserved.
//

#import "ZBSortSegmentView.h"
#import "CommonColor.h"

@interface ZBSortSegmentView()
-(void)buttonPressed:(UIButton*)sender;
@end

@implementation ZBSortSegmentView

@synthesize highlightIndex=_highlightIndex;
@synthesize delegate = _delegate;


-(id)initWithFrame:(CGRect)frame titleArray:(NSArray*)titles; {
    if (self=[super initWithFrame:frame]) {
        _upStatusArray =[[NSMutableArray alloc] initWithCapacity:[titles count]];
        _buttonArray =[[NSMutableArray alloc] initWithCapacity:[titles count]];
        _arrowArray =[[NSMutableArray alloc] initWithCapacity:[titles count]];
       
        for (int i=0; i<[titles count]; i++) {
            UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font =[UIFont boldSystemFontOfSize:12.0f];
            [button setTitleColor:[CommonColor darkBrown] forState:UIControlStateNormal];
            [button setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            
            UIImageView *imageView =[[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
            [self addSubview:imageView];
            
            [_buttonArray addObject:button];
            [_upStatusArray addObject:[NSNumber numberWithBool:NO]];
            [_arrowArray addObject:imageView];
        }
        
        UIImageView *imageView = [_arrowArray objectAtIndex:_highlightIndex];
        imageView.highlighted = YES;
    }
    return self;
}

-(void)dealloc {
    [_upStatusArray release];
    [_buttonArray release];
    [_arrowArray release];
    [super dealloc];    
}

-(void)setLeftSegmentImage:(UIImage*)leftImage middleSegmentImage:(UIImage*)middleImage rightSegmentImage:(UIImage*)rightImage {
    if ([_buttonArray count]>0) {
        UIButton * firstButton = [_buttonArray objectAtIndex:0];
        [firstButton setBackgroundImage:leftImage forState:UIControlStateNormal];
        [firstButton setBackgroundImage:leftImage forState:UIControlStateHighlighted];
    }
    if ([_buttonArray count]>1) {
        UIButton * lastButton = [_buttonArray objectAtIndex:[_buttonArray count]-1];
        [lastButton setBackgroundImage:rightImage forState:UIControlStateNormal];
        [lastButton setBackgroundImage:rightImage forState:UIControlStateHighlighted];   
    }
    for (int i=0; i<[_buttonArray count]-1; i++) {
        UIButton * middleButton = [_buttonArray objectAtIndex:[_buttonArray count]-1];
        [middleButton setBackgroundImage:middleImage forState:UIControlStateNormal];
        [middleButton setBackgroundImage:middleImage forState:UIControlStateHighlighted];   
    }
}

-(void)setArrowImage:(UIImage*)arrowImage hightLightImage:(UIImage*)highImage {
    for (UIImageView* imageView in _arrowArray) {
        imageView.image = arrowImage;
        imageView.highlightedImage = highImage;
    }
    arrowSize=arrowImage.size;
    for (UIButton *button in _buttonArray) {
        button.titleEdgeInsets =UIEdgeInsetsMake(0,0,0,arrowSize.width+5);
    }
}

-(BOOL)upStatus {
    return [[_upStatusArray objectAtIndex:_highlightIndex] boolValue];
}

-(BOOL)isSegmentUpAt:(NSUInteger)index {
    return [[_upStatusArray objectAtIndex:index] boolValue];
}

-(void)buttonPressed:(UIButton*)sender {
    NSUInteger index =[_buttonArray indexOfObject:sender];
    if (index==_highlightIndex) {
        BOOL status = [[_upStatusArray objectAtIndex:index] boolValue];
        [_upStatusArray replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:!status]];
        
        UIImageView *arrow = [_arrowArray objectAtIndex:index];
        if (!status) {
            CGAffineTransform transform = CGAffineTransformMakeTranslation(0, arrowSize.height);
            transform = CGAffineTransformScale(transform,1.0,-1.0);
            arrow.transform = transform;   
        } else {
             arrow.transform = CGAffineTransformIdentity;             
        }
    } else {
        UIImageView *arrow = [_arrowArray objectAtIndex:index];
        arrow.highlighted = YES;
       
        arrow = [_arrowArray objectAtIndex:_highlightIndex];
        arrow.highlighted = NO;
        
        _highlightIndex = index;
    }
    if (self.delegate) {
        [self.delegate statusChangedOfSortSegmentView:self];
    }
}

-(void)layoutSubviews {
    CGRect rect = self.bounds;
    CGFloat buttonWidth = rect.size.width/[_buttonArray count];
    CGFloat x = 0;
    CGFloat y = 0;
    for (int i=0; i<[_buttonArray count]; i++) {
        UIButton *button =[_buttonArray objectAtIndex:i];
        button.frame = CGRectMake(x, y, buttonWidth, rect.size.height);
        x += buttonWidth-1;
    }
    x = buttonWidth-arrowSize.width-5;
    y = (rect.size.height-arrowSize.height)/2;
    for (int i=0; i<[_arrowArray count]; i++) {
        UIImageView *arrow =[_arrowArray objectAtIndex:i];
        arrow.frame = CGRectMake(x, y, arrowSize.width, arrowSize.height);
        x += buttonWidth;
    }  
}
@end
